// 允许在需要的地方使用unsafe代码（特别是与C代码交互的部分）
#![allow(unsafe_code)]
#![cfg_attr(test, allow(unsafe_code))]

// 重新导出所有公共API
pub mod core;
pub mod exokernel;
pub mod drivers;
pub mod callback;
pub mod utils;
pub mod memory;

// 导出常用API以便直接使用
pub use core::error::{FFIError, FFIResult};
pub use core::memory::SafeMemoryBuffer;
pub use core::types::{CapabilityId, PhysicalAddress, VirtualAddress, ResourceType, MemoryPermissions};

pub use exokernel::api;
pub use exokernel::capability::Capability;
pub use exokernel::capability::ThreadSafeCapability;
pub use exokernel::memory::{PhysicalMemoryBlock, MemoryMapping};
pub use exokernel::cpu::CpuManager;
pub use exokernel::io::IoPortManager;
pub use exokernel::interrupt::{InterruptManager, DriverInterruptManager};

pub use drivers::DriverManager;

pub use callback::{CallbackId, CallbackHandler, CallbackRegistry};

pub use utils::{StringUtils, NumberUtils, unsafe_cstr_to_string, unsafe_copy_c_string, unsafe_c_string_length, unsafe_compare_c_strings, unsafe_read_u8, unsafe_write_u8, unsafe_read_u16_le, unsafe_write_u16_le, unsafe_read_u32_le, unsafe_write_u32_le, unsafe_read_u64_le, unsafe_write_u64_le, unsafe_set_ptr_value, unsafe_copy_memory_block, unsafe_get_cstr_ref};

// 导出内存管理相关API
pub use memory::{ThreeLevelCache, ConfigOptimizer, RadixTreeAllocator, AsyncIOScheduler, AsyncFile};
pub use memory::{CacheLevel, CachePolicy};

// 安全的Kuya消息包装器
pub struct SafeKuyaMessage {
    data: Vec<u8>,
}

impl SafeKuyaMessage {
    // 创建新的消息
    pub fn new() -> FFIResult<Self> {
        Ok(SafeKuyaMessage {
            data: Vec::new(),
        })
    }
    
    // 获取消息大小
    pub fn size(&self) -> usize {
        self.data.len()
    }
    
    // 添加字节数据
    pub fn append_bytes(&mut self, bytes: &[u8]) -> FFIResult<()> {
        self.data.extend_from_slice(bytes);
        Ok(())
    }
    
    // 添加字符串数据
    pub fn append_string(&mut self, s: &str) -> FFIResult<()> {
        self.data.extend_from_slice(s.as_bytes());
        // 添加空终止符
        self.data.push(0);
        Ok(())
    }
    
    // 获取原始数据指针
    pub fn data_ptr(&self) -> *const u8 {
        self.data.as_ptr()
    }
}